package demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class MyUserServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(MyUserServlet.class
			.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		UserAO uao = new UserAO();

		String trackername = req.getParameter("trackername");
		String password = req.getParameter("password");
		String typeoftracker = req.getParameter("typeoftracker");
		String simno = req.getParameter("simno");
		String imei = req.getParameter("imei");
		String phoneno = req.getParameter("phoneno");

		String passcode = uao.getLastnCharacters(imei);
		PrintWriter out = resp.getWriter();
		
		List<User> imeilist = uao.getAllImei();
		List<User> phonelist = uao.getAllphoneno();

		if (trackername.trim().length() == 0 || password.trim().length() == 0
				|| typeoftracker.trim().length() == 0
				|| simno.trim().length() == 0 || imei.trim().length() == 0
				|| phoneno.trim().length() == 0) {
			out.println("<html><body onLoad=\"alert('Fields cannot be left blank. Please enter all information to register user');window.location.href='http://omnihtrack.appspot.com/AddUser.html';\"></html>");
		} else {
			if (password.trim().length() < 4 || simno.trim().length() < 8
					|| phoneno.trim().length() < 8) {
				out.println("<html><body onLoad=\"alert('Field not fullfilled. Please re-enter information');window.location.href='http://omnihtrack.appspot.com/AddUser.html';\"></html>");
			} else {
				String testNumber = password + simno + imei + phoneno;
				boolean onlyNumber = uao.containsDigit(testNumber);

				boolean validsim = uao.validSim(simno);
				boolean validphone = uao.validphone(phoneno); 
				
				if (onlyNumber == false || validsim == false || validphone == false) {
					out.println("<html><body onLoad=\"alert('Invalid number field entered. Please re-enter information');window.location.href='http://omnihtrack.appspot.com/AddUser.html';\"></html>");
				} else {

					if (phonelist.contains(phoneno)) {
						out.println("<html><body onLoad=\"alert('User phone number Exist. Please register different user phone number');window.location.href='http://omnihtrack.appspot.com/AddUser.html';\"></html>");
					} else {
						if (imeilist.contains(imei)) {

							out.println("<html><body onLoad=\"alert('Tracker Exist. Please register different tracker');window.location.href='http://omnihtrack.appspot.com/AddUser.html';\"></html>");

						} else {

							uao.add(trackername, password, simno, imei,
									typeoftracker, phoneno, passcode);

							String design = "table#loginDetails{border:3px solid black; padding:5px; text-align:center;font-size:18px; width:250px; border-collapse:collapse; height:350px;}"
									+ "table#trackerDetails{border:3px solid black; padding:5px; text-align:center;font-size:18px; width:500px; border-collapse:collapse; height:350px;}"
									+ "tr,td{border:1px solid black;padding:5px;text-align:center;} "
									+ "body{margin-top:-10px;} "
									+ ".ufield{text-align:left; height:35px; font-size:15px; color: #1E90FF;} "
									+ ".data{font-size:30px;}"
									+ ".details{font-size:22px;}"
									+ "#button{margin-left:26%;}"
									+ "#content{margin-left:15%;}"
									+ ".field{text-align:right; width:200px; color: #1E90FF; font-size:17px;}"
									+ ".title{height:130px; text-align:center; background-color: #ADD8E6;} "
									+ "#pageheading{font-size: 5em; color: #6741FF; font-family: sans serif; font-weight: bold; margin-left:10px;}"
									+ "#main{font-size:38px; letter-spacing:9px; color:#0000CD; font-variant: small-caps; font-weight: bold;} "
									+ "#acc{font-size: 38px; letter-spacing: 9px; color: #0000CD; font-variant: small-caps; font-weight: bold;}";

							resp.setContentType("text/html");
							resp.getWriter()
									.println(
											"<html><head><title>My Tracker</title><link href=\"../css/default.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\" /><style>"
													+ design
													+ "</style></head>"
													+ "<body><div id=\"wrapper\"><div id=\"header-wrapper\"><div id=\"header\" style=\"backgound-color:#0078D1;\"><div id=\"logo\"><h1><a href=\"#\">Omni-Health</a></h1></div></div></div>"
													+ "<div id=\"page-wrapper\"><div id=\"page\"><div id=\"content\"><span id=\"pageheading\"><u>Successfully Adding of User !</u></span><br/><br/>"
													+ "<table id=\"mainTable\" style=\"border-width: 0px;\"><tr style=\"border-width: 0px;\"><td style=\"border-width: 0px;\"><table id=\"loginDetails\">"
													+ "<tr><td class=\"title\"><span id=\"acc\">Login Field</span></td></tr>"
													+ "<tr><td class=\"ufield\">Phone Number : </td></tr><tr><td class=\"data\">"
													+ phoneno
													+ "</td></tr>"
													+ "<tr><td class=\"ufield\">Passcode : </td></tr><tr><td class=\"data\">"
													+ passcode
													+ "</td></tr></table></td><td style=\"border-width: 0px;\">"
													+ "<table id=\"trackerDetails\"><tr><td colspan=\"2\" class=\"title\"><table style=\"border-width: 0px;\"><tr style=\"border-width: 0px;\"><td style=\"border-width: 0px;\">"
													+ "<img src=\"images/information.png\" alt=\"InformationField\" width=\"60\" height=\"60\" style=\"vertical-align:middle;\" /></td>"
													+ "<td style=\"border-width: 0px;\"><span id=\"main\">Tracker Information</span></td></tr></table></td></tr>"
													+ "<tr><td class=\"field\">Tracker Name : </td><td class=\"details\">"
													+ trackername
													+ "</td></tr>"
													+ "<tr><td class=\"field\">Tracker Password : </td><td class=\"details\">"
													+ password
													+ "</td></tr>"
													+ "<tr><td class=\"field\">Type of Tracker : </td><td class=\"details\">"
													+ typeoftracker
													+ "</td></tr>"
													+ "<tr><td class=\"field\">Sim Card Number : </td><td class=\"details\">"
													+ simno
													+ "</td></tr>"
													+ "<tr><td class=\"field\">IMEI : </td><td class=\"details\">"
													+ imei
													+ "</td></tr>"
													+ "</table></td></tr></table><br/><br/>"
													+ "<div id=\"button\"><a href=\"http://omnihtrack.appspot.com/AddUser.html\"><button style=\"color:#0000CD; height:30px; font-size:14px; width:200px;\">Add Another User</button></a>"
													+ "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"http://omnihtrack.appspot.com/adminindex.html\"><button style=\"color:#0000CD; height:30px; font-size:14px; width:100px;\">Map</button></a></div></div></div></div></div>"
													+ "<div id=\"footer\"><p>Copyright (c) by Nanyang Polytechnic.</p></div></body></html>");
						}
					}
				}
			}
		}

	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		doGet(req, resp);

	}

}
